﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emergency4.Dal;
using Emergency4.From;
using Emergency4.Session;

namespace Emergency4.From
{
    public partial class EmergencyMaintenanceRequestDetails : Form
    {
        public EmergencyMaintenanceRequestDetails()
        {
            InitializeComponent();
        }

        //紧急情况的 id
        public int id;
        EmergencyMaintenance em = new EmergencyMaintenance();
        string[] header = new string[] { "Part Name", "Amount" };
        Boolean tag = true;

        //提交按钮
        private void button2_Click(object sender, EventArgs e)
        {
            EmergencyMaintenance emergencyMaintenance = new EmergencyMaintenance();
            emergencyMaintenance.ID = id;
            emergencyMaintenance.EMStartDate = startDate.Value;
            //为空提示相关信息
            if (completeDate.CustomFormat != "")
            {
                emergencyMaintenance.EMEndDate = completeDate.Value;
            }
            else
            {
                emergencyMaintenance.EMEndDate = null;
            }
            if (completeDate.Text == "")
            {
                MessageBox.Show("completeDate不能为空！");
                return;
            }
            if (amount.Text == "")
            {
                MessageBox.Show("amount不能为空！");
                return;
            }
            emergencyMaintenance.EMTechnicianNote = technicalNote.Text;
            emergencyMaintenance.PriorityID = em.PriorityID;
            emergencyMaintenance.EMReportDate = em.EMReportDate;
            emergencyMaintenance.OtherConsiderations = em.OtherConsiderations;
            emergencyMaintenance.AssetID = em.AssetID;
            emergencyMaintenance.DescriptionEmergency = em.DescriptionEmergency;

            AdminDal.updateEmergencyMaintenance(emergencyMaintenance);
            AdminDal.removeEmergencyMaintenance(id);

            //变化部分
            ChangedPart changedPart = new ChangedPart();
            changedPart.EmergencyMaintenanceID = id;

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    changedPart.PartID = AdminDal.findAllChangePart(Convert.ToString(dataGridView1.Rows[i].Cells[0].Value));
                    changedPart.Amount = Decimal.Parse(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value));
                }
                AdminDal.insertPart(changedPart);
            }
            MessageBox.Show("OK");
            //提交成功关闭相应的窗口
            this.Close();
        }
        //取消按钮
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //加载 数据
        private void EmergencyMaintenanceRequestDetails_Load(object sender, EventArgs e)
        {
            //
            em = AdminDal.findByEmergencyId(id);
            assetSN.Text = em.Asset.AssetSN;
            assetName.Text = em.Asset.AssetName;
            departmentName.Text = em.Asset.DepartmentLocation.Department.Name;

            if (em.EMStartDate != null)
            {
                completeDate.Value = em.EMStartDate.Value;
                completeDate.Enabled = false;
                startDate.Enabled = false;
                partName.Enabled = false;
                completeDate.Enabled = false;
                amount.Enabled = false;
                addToList.Enabled = false;
                submitButton.Enabled = false;
                tag = false;
            }
            else
            {
                startDate.MinDate = em.EMReportDate;
                completeDate.MinDate = startDate.Value;
                completeDate.Enabled = false;
                completeDate.Format = DateTimePickerFormat.Custom;
                completeDate.CustomFormat = "";
            }


            partName.Items.Clear();
            List<Part> partsList = AdminDal.findAllPartName();
            for (int i = 0; i < partsList.Count; i++)
            {
                partName.Items.Add(partsList[i].Name);
            }


            for (int i = 0; i < header.Length; i++)
            {
                DataGridViewTextBoxColumn c1 = new DataGridViewTextBoxColumn();
                c1.Name = header[i];
                c1.FillWeight = 500;
                dataGridView1.Columns.Add(c1);
            }
            DataGridViewLinkColumn c2 = new DataGridViewLinkColumn();
            c2.Name = "Action";
            //c2.FillWeight = 500;
            dataGridView1.Columns.Add(c2);
            dataGridView1.AllowUserToAddRows = false;
            List<ChangedPart> changePartList = AdminDal.findAllChangePartById(id);
            showAllPartList(changePartList, tag);
            dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);


        }
        //表格数据 展示
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 1 && e.RowIndex >= 0)
            {
                if (dataGridView1.SelectedCells[0].Value.ToString() == "Remove")
                {
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
            }
        }
        //展示所有数据到表格中
        private void showAllPartList(List<ChangedPart> changePartList, bool tag)
        {
            for (int i = 0; i < changePartList.Count; i++)
            {
                if (tag)
                {
                    dataGridView1.Rows.Add(changePartList[i].Part.Name, changePartList[i].Amount, "Remove");
                }
                else
                {
                    dataGridView1.Rows.Add(changePartList[i].Part.Name, changePartList[i].Amount, "");
                }
            }
        }
        //add to List    
        private void button1_Click(object sender, EventArgs e)
        {
            if (partName.Text == "")
            {
                MessageBox.Show("partName不能为空！");
                return;
            }
            if(amount.Text == "")
            {
                MessageBox.Show("amount不能为空！");
                return;
            }
            dataGridView1.Rows.Add(partName.Text, amount.Text, "Remove");
        }
        //开始日期
        private void startDate_ValueChanged(object sender, EventArgs e)
        {
            completeDate.Value = startDate.Value;
            completeDate.Format = DateTimePickerFormat.Custom;
            completeDate.CustomFormat = "";

        }
        //完成日期
        private void completeDate_ValueChanged(object sender, EventArgs e)
        {
            completeDate.CustomFormat = "yyyy/MM/dd";
        }
        //笔记
        private void technicalNote_TextChanged(object sender, EventArgs e)
        {
            if (technicalNote.Text == "")
            {
                completeDate.Enabled = false;
                completeDate.Format = DateTimePickerFormat.Custom;
                completeDate.CustomFormat = "";
            }
            else
            {
                completeDate.Enabled = true;
            }
        }
    }
}
